package cn.xfdzx.android.apps.shop.presenter;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BluePrintContentBean;
import cn.xfdzx.android.apps.shop.bean.CarCountBeen;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.bean.VersionInfoBean;
import cn.xfdzx.android.apps.shop.contract.CarNumContract;
import cn.xfdzx.android.apps.shop.model.CarNumModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class CarNumPresenter extends BasePresenter<CarNumContract.View> implements CarNumContract.Presenter {

    private CarNumContract.Model model;

    public CarNumPresenter() {
        model = new CarNumModel();
    }



    @Override
    public void getVersionInfo() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getVersionInfo()
                .compose(RxScheduler.<VersionInfoBean>Flo_io_main())
                .as(mView.<VersionInfoBean>bindAutoDispose())
                .subscribe(new Consumer<VersionInfoBean>() {
                    @Override
                    public void accept(VersionInfoBean bean) throws Exception {
                        mView.onVersionInfoSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getEquipmentNumber(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getEquipmentNumber(map)
                .compose(RxScheduler.<VersionInfoBean>Flo_io_main())
                .as(mView.<VersionInfoBean>bindAutoDispose())
                .subscribe(new Consumer<VersionInfoBean>() {
                    @Override
                    public void accept(VersionInfoBean bean) throws Exception {
                        mView.onEquipmentNumberSuccess(bean);
                       // mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                       // mView.hideLoading();
                    }
                });
    }

    @Override
    public void getPrintingContent(String order_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        //mView.showLoading();
        model.getPrintingContent(order_id)
                .compose(RxScheduler.<BluePrintContentBean>Flo_io_main())
                .as(mView.<BluePrintContentBean>bindAutoDispose())
                .subscribe(new Consumer<BluePrintContentBean>() {
                    @Override
                    public void accept(BluePrintContentBean bean) throws Exception {
                        mView.getPrintingContentSuccess(bean);
                        // mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        // mView.hideLoading();
                    }
                });
    }


    @Override
    public void carNum(String index_status, String store_id) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.carNum(index_status, store_id)
                .compose(RxScheduler.<CarCountBeen>Flo_io_main())
                .as(mView.<CarCountBeen>bindAutoDispose())
                .subscribe(new Consumer<CarCountBeen>() {
                    @Override
                    public void accept(CarCountBeen bean) throws Exception {
                        mView.onCarNumSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    /**
     * IM登录
     * */
    @Override
    public void getLiveImLogin(String id,String type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getLiveImLogin(id,type)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.getLiveImLoginSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
